package com.uniriho.lxr9100ble.service;

public class ArrayQueue {

    private int maxSize;
    private int front;//头
    private int rear;//尾
    private int size;//数据大小
    private volatile byte[] arr;
    //初始化
    public ArrayQueue(int arrmaxSize) {
        maxSize = arrmaxSize;
        arr = new byte[maxSize];
        front = -1;
        rear = -1;
        size = 0;
    }
    //判断队列满
    public boolean isFull() {
        return (rear+1) == maxSize;
    }
    //判断队列kong
    public boolean isEmpty() {
        return rear == front;
    }
    //添加数据
    public void addQueue(byte[] n) {
        if(isFull()) {//判满
            LogUtils.debug("addQueue","队列满");
            return;
        }
        LogUtils.debug("getQueue","已写队列:"+ MyUtils.hex2DebugHexString(n));
        for (byte b : n) {
            rear = (rear+1)%maxSize;
            arr[rear] = b;
            size++;
        }
    }
    //出队列
    public byte[] getQueue(int count) {
        if(isEmpty()) {//判空
            LogUtils.debug("getQueue","队空");
            return null;
        }

        if (count >size) {
            count = size;
        }
        byte[] getByte = new byte[count];
        for (int i = 0; i < count; i++) {
            front = (front+1)%maxSize;
            getByte[i] = arr[front];
            size--;
        }
        LogUtils.debug("getQueue","已出队列:"+ MyUtils.hex2DebugHexString(getByte));
        return getByte;
    }
}
